import secrets
import argparse


def read_words(filepath: str) -> list[str]:
    with open(filepath, encoding="utf-8") as file:
        words = [word for word in file.read().split("\n") if word]
    return words


def get_words(source: list[str], count: int = 6) -> list[str]:
    return [secrets.choice(source) for _ in range(count)]


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-w", "--words", type=int, default=6, help="Words amount. Default: %(default)s"
    )
    parser.add_argument(
        "-r", "--results", type=int, default=1, help="Results. Default: %(default)s"
    )
    parser.add_argument(
        "-f",
        "--file",
        type=str,
        default="xkcd_wordlist.txt",
        help="Wordlist file, if not set prints out to console. Default: %(default)s",
    )
    return parser.parse_args()


def main():
    args = parse_args()

    words = read_words(args.file)

    for _ in range(args.results):
        print(" ".join(get_words(words, args.words)))


if __name__ == "__main__":
    main()
